package edu.xpu.project.user.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.xpu.project.user.entity.ProjectUser;
import edu.xpu.project.user.service.ProjectUserService;

/**
 * 系统用户控制器
 * @author liukang
 *
 */
@Controller
@RequestMapping(value="/user")
public class ProjectUserController {
	
	@Autowired
	private ProjectUserService projectUserService;
	
	/**
	  * @MethodName: userDetails
	  * @Description: 人员详情
	  * @Author liukang
	  * @date 2017年5月23日  下午12:13:13
	 */
	@RequestMapping(value="userDetails/{id}")
	public String userDetails(@PathVariable(value="id") Long id, ModelMap modelMap) {
		ProjectUser projectUser = projectUserService.get(id);
		modelMap.addAttribute("projectUser", projectUser);
		return "student/userDetail";
	}
	
	/**
	 * @describe 人员删除
	 * @date 2017年5月22日
	 * @author liukang
	 */
	@ResponseBody
	@RequestMapping(value="userDel/{id}")
	public String userDel(@PathVariable(value="id") Long id) {
		try {
			projectUserService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return "{\"status\":\"error\"}";
		}
		return "{\"status\":\"success\"}";
	}
	
	@RequestMapping(value="toTeacherAdd")
	public String toTeacherAdd() {
		
		return "teacher/teacherAdd";
	}
	
	/**
	 * @describe 学生信息保存
	 * @date 2017年4月18日
	 * @author liukang
	 */
	@RequestMapping(value="/studentAdd")
	public String studentAdd(ProjectUser projectUser, ModelMap modelMap) {
		ProjectUser saveObj = null;
		if (projectUser.getId() == null) {
			//添加系统属性
			projectUser.setCreateDate(new Date());
			saveObj = projectUser;
		} else {
			ProjectUser oldProjectUser = projectUserService.get(projectUser.getId());
			oldProjectUser.setName(projectUser.getName());
			oldProjectUser.setUserNo(projectUser.getUserNo());
			oldProjectUser.setSex(projectUser.getSex());
			oldProjectUser.setDeptName(projectUser.getDeptName());
			oldProjectUser.setMajorName(projectUser.getMajorName());
			saveObj = oldProjectUser;
		}
		projectUserService.saveOrUpdate(saveObj);
		modelMap.addAttribute("userType", saveObj.getUserType());
		return "redirect:studentList";
	}
	
	/**
	 * @describe 去添加学生信息页面
	 * @date 2017年4月18日
	 * @author liukang
	 */
	@RequestMapping(value="/toStudentAdd")
	public String toStudentAdd() {
		
		return "student/userAdd";
	}
	
	/**
	 * 用户列表展示
	 * @param pageNumber
	 * @param pageSize
	 * @param projectUser
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value="/studentList")
	public String studentList(@RequestParam(value="pageNumber", defaultValue="1", required=false) String pageNumber,
			@RequestParam(value="pageSize", defaultValue="10", required=false) String pageSize,
			ProjectUser projectUser, ModelMap modelMap) {
		Page<ProjectUser> pageByCriterias = projectUserService.getPageByCriterias(pageNumber, pageSize, projectUser);
		//将各个参数传到前端
		modelMap.addAttribute("page", pageByCriterias)
				.addAttribute("searchVal", projectUser)
				.addAttribute("number", pageNumber)
				.addAttribute("size", pageSize);
		return "student/userList";
	}

}
